﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HHCommon;

namespace HHWeb.SystemManage.PageKeys
{
    public partial class Dispose : Base
    {
        HHBLL.PageKeys pagekey = new HHBLL.PageKeys();
        HHModel.PageKeys pagemode = new HHModel.PageKeys();
        HHCommon.Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHBLL.Pages pages = new HHBLL.Pages();

        //变量定义
        public string Title = string.Empty;
        public string KeyWord = string.Empty;
        public string Contents = string.Empty;
        public int Sort =0;
        public int Status =1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public int PageIndex =0;
        public string PageName = string.Empty;
        public StringBuilder PageInfo = new StringBuilder();
        public string Command = "Add";
        protected void Page_Load(object sender, EventArgs e)
        {
            string Message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Add":
                            Message = Add();
                            break;
                        case "Update":
                            Message = Update();
                            break;
                        default:
                            break;
                    }
                    if (Message.EndsWith("成功"))
                        MessageBox.ShowAndRedirect(this.Page, Message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, Message, "javascript:history(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int pagepowerid = int.Parse(Request.QueryString["ID"].ToString());
                        pagemode = pagekey.GetModel(pagepowerid);
                        if (pagemode != null)
                        {
                            PageIndex = Convert.ToInt32(pagemode.PageIndex);
                            PageName = pagemode.PageName;
                            Title = pagemode.Title;
                            KeyWord = pagemode.KeyWord;
                            Contents = pagemode.Contents;
                            Sort = Convert.ToInt32(pagemode.Sort);
                            Status = Convert.ToInt32(pagemode.Status);
                            Creater = pagemode.Creater;
                            CreateTime = Convert.ToDateTime(pagemode.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                //获取页面名称（下拉框）
                List<HHModel.Pages> list = pages.GetModelList(" Status=1");
                for (int i = 0; i < list.Count; i++)
                {
                    PageInfo.Append("<option value=\"" + list[i].ID + "\">" + list[i].PageAlias + "</option>");
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //页面关键字添加
        public string Add()
        {
            pagemode.Title = Request.Form["Title"].ToString();
            pagemode.KeyWord = Request.Form["KeyWord"].ToString();
            pagemode.Contents = Request.Form["Contents"].ToString();
            pagemode.PageIndex =Convert.ToInt32(Request.Form["PageName"]);
            pagemode.PageName=Request.Form["HDPageName"].ToString();
            pagemode.Sort = Convert.ToInt32(Request.Form["Sort"]);
            pagemode.Status = Convert.ToInt32(Request.Form["Status"]);
            pagemode.Creater = Request.Form["Creater"].ToString();
            pagemode.CreateTime =Convert.ToDateTime( Request.Form["CreateTime"].ToString());
            if (pagekey.Add(pagemode) > 0)
                return "添加成功";
            else
                return "添加失败";
        }
        #endregion

        #region//页面关键字修改
        public string Update()
        {
            pagemode.ID = int.Parse(Request.QueryString["ID"].ToString());
            pagemode.Title = Request.Form["Title"].ToString();
            pagemode.KeyWord = Request.Form["KeyWord"].ToString();
            pagemode.Contents = Request.Form["Contents"].ToString();
            pagemode.PageIndex = Convert.ToInt32(Request.Form["PageName"]);
            pagemode.PageName = Request.Form["HDPageName"].ToString();
            pagemode.Sort = Convert.ToInt32(Request.Form["Sort"]);
            pagemode.Status = Convert.ToInt32(Request.Form["Status"]);
            pagemode.Creater = Request.Form["Creater"].ToString();
            pagemode.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            if (pagekey.Update(pagemode))
                return "修改成功";
            else
                return "修改失败";
        }
        #endregion
    }
}